package com.app.lxx.pregnancyberth.service;


import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class DataBus {
    private static Executor executor = Executors.newSingleThreadExecutor();
    private static Handler handler = new Handler(Looper.getMainLooper());

    public static void executeSubThread(Runnable runnable) {
        executor.execute(runnable);
    }

    public static void executeMainThread(Runnable runnable) {
        handler.post(runnable);
    }

    private static final String TAG = "DataBus";
    private static Map<String, ArrayList<Subscriber>> subscriberMap = new HashMap<>();

    /**
     * 订阅
     *
     * @param tag            用于订阅的主题
     * @param dataSubscriber 消息回调
     */
    public static void subscribe(String tag, Subscriber dataSubscriber) {
        LogUtils.e("subscribe:" + tag);
        if (subscriberMap.containsKey(tag)) {
            ArrayList<Subscriber> sbs = subscriberMap.get(tag);
            if (sbs == null) {
                sbs = new ArrayList<>();
            }
            sbs.add(dataSubscriber);
            subscriberMap.put(tag, sbs);
            Log.e("sssssButtonService", "if subscribe tag="+tag);
            Log.e("sssssButtonService", "else subscribe subscriberMap="+subscriberMap);

        } else {
            ArrayList<Subscriber> sbs = new ArrayList<>();
            sbs.add(dataSubscriber);
            Log.e("sssssButtonService", "else subscribe tag="+tag);
            subscriberMap.put(tag, sbs);
            Log.e("sssssButtonService", "else subscribe-subscriberMap="+subscriberMap);

        }
    }

    /**
     * 取消订阅
     *
     * @param tag 订阅主题
     */
    public static void unSubscribe(String tag) {
        if (subscriberMap.get(tag) != null) {
            subscriberMap.remove(tag);
            Log.e("sssssButtonService", "unSubscribe="+tag);
            LogUtils.e("unSubscribe:" + tag);
        } else {
            LogUtils.e("sssssButtonService unSubscribe: not found " + tag);
        }
    }

    /**
     * 发消息
     *
     * @param tag 主题
     * @param msg 数据
     */
    public static void post(String tag, Message msg) {
        ArrayList<Subscriber> msgSubscribers = subscriberMap.get(tag);
        Log.e("sssssButtonService", "tag="+tag);
        Log.e("sssssButtonService", "subscriberMap="+subscriberMap);
        Log.e("sssssButtonService", "msgSubscribers!= null="+msgSubscribers);

        if (msgSubscribers != null) {
            LogUtils.e("post:" + tag + " " + GsonUtils.toJson(msg));
            ArrayList<Subscriber> temp = new ArrayList<>();
            temp.addAll(msgSubscribers);
            for (Subscriber dataSubscriber : msgSubscribers) {
                executeMainThread(new Runnable() {
                    @Override
                    public void run() {
                        dataSubscriber.onReceive(msg);
                    }
                });
            }

            for (Subscriber msgSubscriber : temp) {
                if (msgSubscriber.disposable) {
                    temp.remove(msgSubscriber);
                }
            }
            Log.e("sssssButtonService", "post-subscriberMap-tag="+tag);
            subscriberMap.put(tag, temp);
        } else {
            Log.e("sssssButtonService", "no DataSubscriber for tag");

        }
    }

    /**
     * 回调抽象类
     */
    public abstract static class Subscriber {
        private boolean disposable = false;

        /**
         * 回调方法
         *
         * @param msg 数据
         */
        protected abstract void onReceive(Message msg);

        /**
         * 构造方法
         *
         * @param disposable 用后即弃(为true时 回调一次就丢弃,适用于某种需要只用一次的特殊场景使用)
         */
        public Subscriber(boolean disposable) {
            this.disposable = disposable;
        }

        public Subscriber() {
        }
    }

    /**
     * 消息
     */
    public static class Message {
        public int what;
        public int arg1;
        public String arg2;
        public Object obj;

        public Message(int what) {
            this.what = what;
        }

        public Message(String arg2) {
            this.arg2 = arg2;
        }

        public Message(Object obj) {
            this.obj = obj;
        }

        public Message(int what, int arg1) {
            this.what = what;
            this.arg1 = arg1;
        }

        public Message(int what, int arg1, String arg2) {
            this.what = what;
            this.arg1 = arg1;
            this.arg2 = arg2;
        }

        public Message(int what, int arg1, String arg2, Object obj) {
            this.what = what;
            this.arg1 = arg1;
            this.arg2 = arg2;
            this.obj = obj;
        }

        public int getWhat() {
            return what;
        }

        public void setWhat(int what) {
            this.what = what;
        }

        public int getArg1() {
            return arg1;
        }

        public void setArg1(int arg1) {
            this.arg1 = arg1;
        }

        public String getArg2() {
            return arg2;
        }

        public void setArg2(String arg2) {
            this.arg2 = arg2;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }
}
